/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */



package studentdata;

import java.util.*;
import java.io.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;



/**
 *
 * @author Administrator
 */
public class Student{
    public static enum workIDs
    {
        ID_NONE,
        ID_MANAGER,
        ID_RECEPTIONIST,
        ID_ADMINISTRATOR,
        ID_STUDENT
    }

    public static workIDs StrToWorkID(String str)
    {
        if(str.equals("ID_NONE")) return workIDs.ID_NONE;
        if(str.equals("ID_MANAGER")) return workIDs.ID_MANAGER;
        if(str.equals("ID_RECEPTIONIST")) return workIDs.ID_RECEPTIONIST;
        if(str.equals("ID_ADMINISTRATOR")) return workIDs.ID_ADMINISTRATOR;
        if(str.equals("ID_STUDENT")) return workIDs.ID_STUDENT;
        return workIDs.ID_NONE;
    }

    public class Info
    {
        byte originID;
        byte destinationID;
        String question;
        String reply;
    }

    private String name;
    private String surname;
    private String peselNumber;
    private String address;
    private String faculty;
    private String dormitoryCode;
    private String roomNumber;

    //equipment
    private boolean lamp;
    private boolean blanket;
    private boolean bedding;
    private boolean phone;

    //internet data
    private String macNumber;
    private boolean accountCreated;
    private String adminInformation;

    Vector additionalInfo = new Vector();
    Vector workflow = new Vector();

    public Vector getWorkflow() {
        return workflow;
    }

    public void setWorkflow(Vector workflow) {
        this.workflow = workflow;
    }

    public Student(String name, String surname)
    {
        this.name = name;
        this.surname = surname;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    private void writeName(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<name>\n");
        xml.write(ind + "\t" + this.name + "\n");
        xml.write(ind + "</name>\n");
    }

    public boolean isAccountCreated() {
        return accountCreated;
    }

    public void setAccountCreated(boolean accountCreated) {
        this.accountCreated = accountCreated;
    }

    public Vector getAdditionalInfo() {
        return additionalInfo;
    }

    public void setAdditionalInfo(Vector additionalInfo) {
        this.additionalInfo = additionalInfo;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    private void writeAddress(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<address>\n");
        xml.write(ind + "\t" + this.address + "\n");
        xml.write(ind + "</address>\n");
    }

    public String getAdminInformation() {
        return adminInformation;
    }

    public void setAdminInformation(String adminInformation) {
        this.adminInformation = adminInformation;
    }

    public boolean isBedding() {
        return bedding;
    }

    public void setBedding(boolean bedding) {
        this.bedding = bedding;
    }

    public boolean isBlanket() {
        return blanket;
    }

    public void setBlanket(boolean blanket) {
        this.blanket = blanket;
    }

    public String getDormitoryCode() {
        return dormitoryCode;
    }

    public void setDormitoryCode(String dormitoryCode) {
        this.dormitoryCode = dormitoryCode;
    }

    private void writeDormitory(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<dormitory roomNumber='" + this.roomNumber + "'>\n");
        xml.write(ind + "\t" + this.dormitoryCode + "\n");
        xml.write(ind + "</dormitory>\n");
    }

    public String getFaculty() {
        return faculty;
    }

    public void setFaculty(String faculty) {
        this.faculty = faculty;
    }

    private void writeFaculty(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<faculty>\n");
        xml.write(ind + "\t" + this.faculty + "\n");
        xml.write(ind + "</faculty>\n");
    }

    public boolean isLamp() {
        return lamp;
    }

    public void setLamp(boolean lamp) {
        this.lamp = lamp;
    }
    
    public String getMacNumber() {
        return macNumber;
    }

    public void setMacNumber(String macNumber) {
        this.macNumber = macNumber;
    }

    public String getPeselNumber() {
        return peselNumber;
    }

    public void setPeselNumber(String peselNumber) {
        this.peselNumber = peselNumber;
    }

    private void writePeselNumber(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<peselNumber>\n");
        xml.write(ind + "\t" + this.peselNumber + "\n");
        xml.write(ind + "</peselNumber>\n");
    }

    public boolean isPhone() {
        return phone;
    }

    public void setPhone(boolean phone) {
        this.phone = phone;
    }

    public String getRoomNumber() {
        return roomNumber;
    }

    public void setRoomNumber(String roomNumber) {
        this.roomNumber = roomNumber;
    }

    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

    private void writeSurname(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<surname>\n");
        xml.write(ind + "\t" + this.surname + "\n");
        xml.write(ind + "</surname>\n");
    }

    public String createFileName()
    {
        return name.toLowerCase() + "_" + surname.toLowerCase() + ".xml";
    }
    
    public void writeToXML(String fileName) throws IOException
    {
        BufferedWriter xml = new BufferedWriter(new FileWriter(fileName));

        xml.write("<student>\n");

        writePersonalData(xml, 1);

        writeDormitoryData(xml, 1);
        
        writeEquipment(xml, 1);

        writeAdmin(xml, 1);

        writeWorkflow(xml, 1);

        xml.write("</student>\n");

        xml.close();
    }

    private String indent(int len)
    {
        String result = "";
        for(int i=0; i<len; i++)
        {
            result += "\t";
        }
        return result;
    }

    private void writePersonalData(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<personalData>\n");
        writeName(xml, indentLen+1);
        writeSurname(xml, indentLen+1);
        writePeselNumber(xml, indentLen+1);
        writeAddress(xml, indentLen+1);
        xml.write(ind + "</personalData>\n");
    }

    private void writeDormitoryData(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<universityData>\n");
        writeFaculty(xml, indentLen+1);
        writeDormitory(xml, indentLen+1);
        xml.write(ind + "</universityData>\n");
    }
    private void writeEquipment(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<equipment>\n");
        xml.write(ind + "\t" + "<lamp>\n");
        xml.write(ind + "\t\t" + this.lamp + "\n");
        xml.write(ind + "\t" + "</lamp>\n");
        xml.write(ind + "\t" + "<blanket>\n");
        xml.write(ind + "\t\t" + this.blanket + "\n");
        xml.write(ind + "\t" + "</blanket>\n");
        xml.write(ind + "\t" + "<phone>\n");
        xml.write(ind + "\t\t" + this.phone + "\n");
        xml.write(ind + "\t" + "</phone>\n");
        xml.write(ind + "\t" + "<bedding>\n");
        xml.write(ind + "\t\t" + this.bedding + "\n");
        xml.write(ind + "\t" + "</bedding>\n");


        xml.write(ind + "</equipment>\n");
    }
    private void writeAdmin(BufferedWriter xml,int indentLen) throws IOException
    {
    String ind = indent(indentLen);
        xml.write(ind + "<network>\n");
        xml.write(ind + "\t" + "<macNumber>\n");
        xml.write(ind + "\t\t" + this.macNumber + "\n");
        xml.write(ind + "\t" + "</macNumber>\n");
        xml.write(ind + "</network>\n");
    }
    private void writeWorkflowNode(BufferedWriter xml, int index, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        WorkflowNode node = (WorkflowNode)workflow.elementAt(index);
        xml.write(ind + "<workflowNode fromID='" + node.fromID.toString()
                + "' recipientID='" + node.recipientID.toString() + "' toID='"
                + node.toID.toString() + "' sendDate='" + node.sendDate + "'>\n");
        xml.write(ind + "\t" + node.recipientName + "\n");
        xml.write(ind + "</workflowNode>\n");
    }

    private void writeWorkflow(BufferedWriter xml, int indentLen) throws IOException
    {
        String ind = indent(indentLen);
        xml.write(ind + "<workflow>\n");
        for(int i=0; i<workflow.size(); i++)
        {
            writeWorkflowNode(xml, i, indentLen+1);
        }
        xml.write(ind + "</workflow>\n");
    }

    public void readFromXML()
    {
        try{
            XMLReader xr = XMLReaderFactory.createXMLReader();
            XMLHandler handler = new XMLHandler();
            handler.setStudent(this);
            xr.setContentHandler(handler);
            xr.setErrorHandler(handler);

            try{
                FileReader file = new FileReader(createFileName());
                xr.parse(new InputSource(file));
            }catch(FileNotFoundException e)
            {
                System.out.print("Nie znaleziono pliku");
            }catch(IOException e)
            {
                System.out.print("Nie mona odczytac pliku");
            }


        }catch(SAXException e)
        {
            e.printStackTrace();
        }
    }
}
